﻿using System;
using System.Collections.Generic;
using System.Linq;

using Sustainalytics.ClientDW.Entities;
using Sustainalytics.Entities;
using Sustainalytics.Entities.ESGRatings;

namespace Sustainalytics.HistoricalData.ComputationTool
{
    public class MatrixStructure : IEntity
    {
        public Guid Id { get; set; }

        public List<ElementStructure> IndicatorsDetails { get; set; }

        public string Name { get; set; }

        public static MatrixStructure MapFromWeightMatrix(WeightMatrix matrix, IList<IndicatorTemplate> templates)
        {
            var ms = new MatrixStructure
                         {
                             Id = matrix.Id, 
                             Name = matrix.Name, 
                             IndicatorsDetails = new List<ElementStructure>()
                         };

            // todo insert in UploadMatrixStructureToDistributedCache
            foreach (var indic in matrix.IndicatorsNumbers)
            {
                var template = templates.FirstOrDefault(x => x.Number == indic);
                if (template != null)
                {
                    var indicator = new ElementStructure
                                        {
                                            IndicatorName = indic, 
                                            ThemeName = template.ThemeName, 
                                            ThemeSymbol = template.ThemeName.Substring(0, 1), 
                                            ClusterName = template.IndicatorCategory
                                        };
                    ms.IndicatorsDetails.Add(indicator);
                }
            }

            return ms;
        }

        public class ElementStructure
        {
            public string ClusterName { get; set; }

            public string ClusterSymbol
            {
                get
                {
                    return this.GetSymbol(this.ClusterName);
                }
            }

            public string IndicatorName { get; set; }

            public string ThemeName { get; set; }

            public string ThemeSymbol { get; set; }

            private string GetSymbol(string indicatorCluster)
            {
                var mappingDictionary = new Dictionary<string, string>
                                            {
                                                { "Disclosure", "D" }, 
                                                {
                                                    "Qualitative Performance - Controversies", 
                                                    "QLP"
                                                }, 
                                                { "Quantitative Performance", "QP" }, 
                                                { "Preparedness", "P" }
                                            };

                if (mappingDictionary.ContainsKey(indicatorCluster))
                {
                    return mappingDictionary[indicatorCluster];
                }

                return string.Empty;
            }
        }
    }
}